package org.one.stone.soup.grfxML.plugin.widget;


import org.one.stone.soup.grfxML.DataImage;
import org.one.stone.soup.grfxML.GeometryImage;
import org.one.stone.soup.grfxML.GrfxMLEngine;
import org.one.stone.soup.grfxML.plugin.SimplePlugin;
import org.one.stone.soup.grfxML.plugin.grfxMLCaster;
import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;

public class ImageGridMapping extends SimplePlugin {


	public static final int ARG_MAP = 0;
	public static final int ARG_WIDTH = 1;
	public static final int ARG_HEIGHT = 2;
	public static final int ARG_CELL_WIDTH = 3;
	public static final int ARG_CELL_HEIGHT = 4;
	public static final int ARG_TARGET = 5;

	org.one.stone.soup.grfxML.DataText map = new org.one.stone.soup.grfxML.DataText();
	org.one.stone.soup.grfxML.DataSize width = new org.one.stone.soup.grfxML.DataSize();
	org.one.stone.soup.grfxML.DataSize height = new org.one.stone.soup.grfxML.DataSize();
	org.one.stone.soup.grfxML.DataSize cellWidth = new org.one.stone.soup.grfxML.DataSize();
	org.one.stone.soup.grfxML.DataSize cellHeight = new org.one.stone.soup.grfxML.DataSize();
	org.one.stone.soup.grfxML.DataNode target;
/**
 * RouteMapping constructor comment.
 */
public ImageGridMapping (GrfxMLEngine engine){
	super(engine);

	target = new org.one.stone.soup.grfxML.DataNode(engine);
}
/**
 * initialize method comment.
 */
public void initialize()
{
 try{
	int cellW = cellWidth.getValue();
	int cellH = cellHeight.getValue();

	for(int loopW=0;loopW<width.getValue();loopW++)
	{
		for(int loopH=0;loopH<height.getValue();loopH++)
		{
			String imageName = map.get(loopW+(loopH*width.getValue())).getValue();
			if(imageName==null)
				break;

			Field image = getEngine().getAPI().getField(imageName);

			if(image==null)
				continue;
			if(!(image instanceof DataImage))
				continue;

			System.out.println("X:"+loopW+" Y:"+loopH+" Add:"+imageName);

			DataImage img = (DataImage)((DataImage)image).getClone();
			img.translate(target.getTranslation());
			((GeometryImage)img.getGeometry(getEngine())).setImage( ((GeometryImage)((DataImage)image).getGeometry(getEngine())).getImage() );
			img.getOffset().setValueX(loopW*cellW,this);
			img.getOffset().setValueY(loopH*cellH,this);
			target.getShape().add(img,this);
		}
	}

	getEngine().getAPI().commit();
 }catch(LockException le){}
}
/**
 * process method comment.
 */
public void process()
{}
/**
 * register method comment.
 */
public void register(DataLinker store)
{

	map = grfxMLCaster.cast(map,getArg(map,ARG_MAP,store));
	width = grfxMLCaster.cast(width,getArg(width,ARG_WIDTH,store));
	height = grfxMLCaster.cast(height,getArg(height,ARG_HEIGHT,store));
	cellWidth = grfxMLCaster.cast(cellWidth,getArg(cellWidth,ARG_CELL_WIDTH,store));
	cellHeight = grfxMLCaster.cast(cellHeight,getArg(cellHeight,ARG_CELL_HEIGHT,store));
	target = grfxMLCaster.cast(target,getArg(target,ARG_TARGET,store));

}
/**
 * replace method comment.
 */
public void replace(Field oldObj, Field newObj)
{

	map = grfxMLCaster.replace(map,oldObj,newObj);
	width = grfxMLCaster.replace(width,oldObj,newObj);
	height = grfxMLCaster.replace(height,oldObj,newObj);
	cellWidth = grfxMLCaster.replace(cellWidth,oldObj,newObj);
	cellHeight = grfxMLCaster.replace(cellHeight,oldObj,newObj);
	target = grfxMLCaster.replace(target,oldObj,newObj);

}
/**
 * stop method comment.
 */
public void stop() {}
}
